OBJECT Report 50010 Adeptris Zip File Test
{
  OBJECT-PROPERTIES
  {
    Date=12/07/15;
    Time=17:01:16;
    Modified=Yes;
    Version List=ZIP1.00;
  }
  PROPERTIES
  {
    ShowPrintStatus=No;
    ProcessingOnly=Yes;
    OnPreReport=BEGIN
                  //Has the User entered a Zip Folder Name
                  IF ZipFolder = '' THEN
                    ERROR(Text002,'Zip File Folder ');

                  //Has the User entered a Zip File Name
                  IF ZipName = '' THEN
                    ERROR(Text002,'Zip File Name ');

                  //If the user requested a Date Time Stamp
                  IF AppendDT THEN
                    TimeStamp := '-' + FORMAT(CURRENTDATETIME,0,'<Year><Month,2><Day,2><Hours24><Minutes,2><Seconds,2>')
                  ELSE
                    TimeStamp := '';

                  //Set the Zip File Name
                  ZipFileName := DELCHR(ZipFolder,'>','\') + '\' + ZipName + TimeStamp +'.zip';

                  //Set the Temp GUID to use as a unique folder name
                  TempGUID := CREATEGUID;

                  //If the Achive Folder Path has been entered then use this folder to create the temporary directory
                  IF ArchiveFolder <> '' THEN
                    XmlFolder := DELCHR(ArchiveFolder,'>','\') + '\' + FORMAT(TempGUID) + '\'
                  ELSE
                    XmlFolder := DELCHR(ZipFolder,'>','\') + '\' + FORMAT(TempGUID) + '\';

                  //Use the Directory Helper to Create the Directory, User must have write permissions
                  IODirectory.CreateDirectory(XmlFolder);

                  //If the user is overwriting the zip file delete it
                  IF FILE.EXISTS(ZipFileName)THEN
                    FILE.ERASE(ZipFileName);

                  //Clear the file counter variable
                  CLEAR(ExportCount);

                  //Call the function to export the Contacts XMLPort and update the counter
                  IF ExportXmlPort(XMLPORT::"Export Contact",'Contact','.txt')THEN
                    ExportCount := ExportCount + 1;

                  //Call the function to export the Segment Contacts XMLPort, update the counter
                  IF ExportXmlPort(XMLPORT::"Export Segment Contact",'Segment-Contact','.txt')THEN
                    ExportCount := ExportCount + 1;

                  //Clear the Dot.Net Zip Variable
                  CLEAR(IOCompress);

                  //Zip all the files in the Temporary Folder to the Zip File if the counter is set
                  IF ExportCount <> 0 THEN
                    IOCompress.CreateFromDirectory(XmlFolder,ZipFileName);

                  //Delete the Temporary Directory recursive to delete files
                  IODirectory.Delete(XmlFolder,TRUE);
                END;

  }
  DATASET
  {
  }
  REQUESTPAGE
  {
    PROPERTIES
    {
    }
    CONTROLS
    {
      { 1000000000;;Container;
                  Name=Options;
                  ContainerType=ContentArea }

      { 1000000004;1;Field  ;
                  Name=Archive Folder;
                  CaptionML=ENU=Archive Folder (Optional);
                  SourceExpr=ArchiveFolder;
                  OnValidate=BEGIN
                               //Return the Folder Path from the Input
                               ArchiveFolder := FileMgmt.GetDirectoryName(ArchiveFolder);
                             END;

                  OnLookup=BEGIN
                             //Open a dialog window to browse for the folder
                             ArchiveFolder := FileMgmt.BrowseForFolderDialog('Archive File Folder','',TRUE);
                           END;
                            }

      { 1000000002;1;Field  ;
                  Name=Zip Folder;
                  CaptionML=ENU=Zip File Folder;
                  SourceExpr=ZipFolder;
                  OnValidate=BEGIN
                               //Return the Folder Path from the Input
                               ZipFolder := FileMgmt.GetDirectoryName(ZipFolder);
                             END;

                  OnLookup=BEGIN
                             //Open a dialog window to browse for the folder
                             ZipFolder := FileMgmt.BrowseForFolderDialog('Zipped File Folder','',TRUE);
                           END;
                            }

      { 1000000003;1;Field  ;
                  Name=Zip Name;
                  CaptionML=ENU=Zip File Name;
                  SourceExpr=ZipName;
                  OnValidate=VAR
                               i@1000000000 : Integer;
                             BEGIN
                               IF ZipName <> '' THEN BEGIN
                                 //Make sure no invalid characters are in the file name
                                 ZipName :=  FileMgmt.StripNotsupportChrInFileName(ZipName);

                                 //If the user as added a file extention then remove it
                                 i := STRPOS(ZipName,'.');
                                 IF i > 0 THEN
                                     ZipName := COPYSTR(ZipName,1,i-1);

                                 //Make sure the file name is valid, giving the user a chance to re-enter
                                 IF NOT FileMgmt.IsValidFileName(ZipName) THEN BEGIN
                                   MESSAGE(Text001);
                                   ZipName := '';
                                 END;
                               END;
                             END;
                              }

      { 1000000001;1;Field  ;
                  Name=Append DateTime;
                  CaptionML=ENU=Append Date Time Stamp;
                  SourceExpr=AppendDT }

    }
  }
  LABELS
  {
  }
  CODE
  {
    VAR
      FileMgmt@1000000008 : Codeunit 419;
      IOCompress@1000000007 : DotNet "'System.IO.Compression.FileSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.Compression.ZipFile";
      IODirectory@1000000011 : DotNet "'mscorlib'.System.IO.Directory" RUNONCLIENT;
      TempGUID@1000000017 : GUID;
      XmlFolder@1000000015 : Text;
      XmlFileName@1000000004 : Text;
      ZipFileName@1000000002 : Text;
      ZipFolder@1000000009 : Text;
      ZipName@1000000010 : Text;
      Text001@1000000001 : TextConst 'ENU=Invalid File Name';
      Text002@1000000005 : TextConst 'ENU=The %1 Cannot be Empty';
      ArchiveFolder@1000000012 : Text;
      ArchiveFileName@1000000013 : Text;
      TimeStamp@1000000003 : Text;
      AppendDT@1000000016 : Boolean;
      ExportCount@1000000006 : Integer;

    PROCEDURE ExportXmlPort@1000000000(DataportNo@1000000000 : Integer;NewFileName@1000000001 : Text;Extention@1000000005 : Text) : Boolean;
    VAR
      XmlFile@1000000004 : File;
      XmlStream@1000000003 : OutStream;
      IsExported@1000000002 : Boolean;
    BEGIN
      //Set the File name to Export
      XmlFileName := DELCHR(XmlFolder,'>','\') + '\' + NewFileName + TimeStamp + Extention;

      //If replacing the file delete the old file
      IF FILE.EXISTS(XmlFileName)THEN
        FILE.ERASE(XmlFileName);

      //If archiving the files the set the Archive file name
      IF ArchiveFolder <> '' THEN BEGIN
        ArchiveFileName := DELCHR(ArchiveFolder,'>','\') + '\' + NewFileName + TimeStamp + Extention;

        //If replacing the Archived Version then delete the old file
        IF FILE.EXISTS(ArchiveFileName)THEN
          FILE.ERASE(ArchiveFileName);
      END ELSE
        ArchiveFileName := '';

      //Clear the OutStream variable
      CLEAR(XmlStream);

      //Create the file and export the XMLPort into the Files OutStream
      XmlFile.CREATE(XmlFileName);
      XmlFile.CREATEOUTSTREAM(XmlStream);
      IsExported := XMLPORT.EXPORT(DataportNo, XmlStream);
      XmlFile.CLOSE;

      //If the Export Ran ok then Archive the file if required
      IF IsExported AND (ArchiveFileName <> '')THEN
        FILE.COPY(XmlFileName,ArchiveFileName);

      //Return the IsExported Boolean
      EXIT(IsExported);
    END;

    BEGIN
    {
      //Use these a in Codeunit
      //XmlFolder := FileMgmt.GetDirectoryName(XmlFolder);
      //ZipFolder := FileMgmt.GetDirectoryName(ZipFolder);
      //ArchiveFolder := FileMgmt.GetDirectoryName(ArchiveFolder);
      //FileMgmt.StripNotsupportChrInFileName(ZipName);
    }
    END.
  }
  RDLDATA
  {
  }
}

